package com.bluesky.jwf.component.validator;

import java.io.IOException;
import java.io.Writer;

import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.captcha.Captcha;
import com.bluesky.jwf.component.captcha.CaptchasDotNetService;

/**
 * the target must be a captcha component
 * 
 * @author jack
 * 
 */
public class CaptchaValidator extends Validator {
	private Captcha captcha;

	public CaptchaValidator() {

	}

	@Override
	public void init(String html) {

		super.init(html);
		setMessage("must same with the word in image");
	}

	@Override
	public void setTarget(Component target) {

		super.setTarget(target);
		captcha = (Captcha) target;

	}

	@Override
	public void validate() {

		boolean b = captcha.isHuman();
		setResult(b);
	}
	
	@Override
	public void writeHtml(Writer writer) throws IOException {
	
		super.writeHtml(writer);
	}

	public static void main(String[] args) {
		ComponentFactory.setCharEncoding("utf-8");
		ComponentFactory
				.setConfigFileName("/com/bluesky/jwf/component/validator/jwf.properties");

		CaptchaValidator v = (CaptchaValidator) ComponentFactory.getInstance()
				.createComponent(CaptchaValidator.class.getSimpleName());
		v.setResult(true);
		System.out.println("true:" + v.toHtml());

		v.setResult(false);
		System.out.println("false:" + v.toHtml());

	}
}